//
//  JJUploadFileApi.m
//  DemoPro
//
//  Created by vpjacob on 2017/5/23.
//  Copyright © 2017年 vpjacob. All rights reserved.
//

#import "JJUploadFileApi.h"
#import "UIImage+Resize.h"
#import <YTKBatchRequest.h>
#import <AFURLRequestSerialization.h>

@interface JJUploadFileApi ()
@property (nonatomic, strong)UIImage *image;
@end

@implementation JJUploadFileApi
- (instancetype)initWithImage:(UIImage *)image {
    if (self = [super init]) {
        _image = image;
    }
    return self;
}

- (NSString *)requestUrl{
    return @"http://123.56.3.47:8080/testIos/file/uploadPic2.do";
}

- (id)requestArgument {
    NSInteger userId = 22;
    NSDictionary *dic = @{@"user_id":@(userId)};
    [self.baseMuDic addEntriesFromDictionary:dic];
    return self.baseMuDic;
}

- (AFConstructingBlock)constructingBodyBlock {
    if (_image) {
        return ^(id<AFMultipartFormData> formData) {
            NSData *data = [_image dealImageMaxFileSize:600];
            //            NSData *data = UIImageJPEGRepresentation(_image, 0.9);
            NSString *name = @"image.jpg";
            NSString *formKey = @"image";
            NSString *type = @"image/jpeg";
            [formData appendPartWithFileData:data name:formKey fileName:name mimeType:type];
        };
    }
//    if (_videoUrl) {
//        return ^(id<AFMultipartFormData> formData) {
//            NSURL *fileUrl = [NSURL URLWithString:_videoUrl];
//            
//            NSError *error;
//            
//            NSData *data = [NSData dataWithContentsOfURL:fileUrl options:0 error:&error];
//            
//            NSString *name = @"video.mp4";
//            NSString *formKey = @"video";
//            NSString *type = @"video/mp4";
//            [formData appendPartWithFileData:data name:formKey fileName:name mimeType:type];
//        };
//    }
    return nil;
}

- (NSString *)getUploadFileUrl {
    return [self.responseJSONObject objectForKey:@"info"];
}

@end
